<template>
  <div>
    <el-dialog title="单据打印信息" :visible="visible" width="50%" @close="handleCloseBill">
      <!-- 这里可以放置对话框内部的内容 -->
      <el-row :gutter="15">
        <el-form ref="billForm" :model="formDataBill" size="medium" label-width="100px">
          <el-col :span="12">
            <el-form-item label="模板选择">
              <el-select v-model="formDataBill.modelBill" placeholder="请选择单据模板" clearable :style="{ width: '100%' }">
                <el-option v-for="(item, index) in formDataBillList" :key="index" :label="item.label"
                  :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24" style=" display: flex; justify-content: flex-end;">
            <el-form-item size="large">
              <el-button type="primary" @click="printBill">打印</el-button>
              <el-button @click="handleCloseBill">取消</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
    </el-dialog>
    <goodsPrint v-show="false" ref="buyIndexRef"></goodsPrint>
  </div>
</template>

<script>
  import goodsPrint from '@/views/basic/print/printList/print.vue'
  import {
    getCountPrint,
    getWarehouseOutDetailProductPrint //单据打印
  } from "@/api/basic/PersonnelInformation.js";
  export default {
    data() {
      return {
        formDataBillList: [{
          value: 'procurementIn-Low',
          label: '采购入库(低耗类)',
        }, {
          value: 'procurementIn-NotLow',
          label: '采购入库(非低耗类)',
        }, {
          value: 'otherIn',
          label: '其他入库',
        }, {
          value: 'otherInWu',
          label: "其他入库(物料入库单)"
        }, {
          value: 'otherOut',
          label: "其他出库(产品交接单)"
        }, {
          value: 'otherOutMore',
          label: "其他出库(成品多行)"
        }, {
          value: 'otherOutPrtBill',
          label: "其他出库(生产原辅料)"
        }], //单据模板
        formDataBill: {
          modelBill: '',
        }, //模板选择弹框
        billCode: '',
      }
    },
    props: ['visible'],
    components: {
      goodsPrint
    },
    methods: {
      open(row) {
        this.billCode = row;
        this.formDataBill.modelBill = '';
      },
      handleCloseBill() {
        // 通过事件通知父组件更新visible值，触发update:visible事件
        this.$emit('update:visible', false);
        this.$nextTick(() => {
          this.$refs.billForm.resetFields();
        })
      },
      //打印
      printBill() {
        getWarehouseOutDetailProductPrint(this.billCode).then(res => {
          // 创建一个 table 数组，将每一条的指定字段提取到 table 中
          let table = [];
          let source;
          switch (this.formDataBill.modelBill) {
            case 'procurementIn-Low':
              source = res.data.purchaseLow?.[0] || {};
              table = res.data.purchaseLow.map(item => ({
                cmaterialoidCode: item.vbcdef2 || item.gmpCode || item.cmaterialoidCode, //物料代码
                cmaterialoidName: item.cmaterialoidName, //物料名称
                materialspec: item.spec, //规格
                cproductoridName: item.cproductoridName, //生产厂家
                vbatchcode: item.vbatchcode, //批号
                nnum: item.nnum, //实收主数量
                vvendbatchcode: item.vvendbatchcode, //厂家批号
                dvalidate: item.dvalidate, //有效期
                vbdef48: item.vbdef48, //复验期,
                cunitidName: item.cunitidName //单位
              }));
              break;
            case 'procurementIn-NotLow':
              source = res.data.purchaseNotLow?.[0] || {};
              table = res.data.purchaseNotLow.map(item => ({
                cmaterialoidCode: item.vbcdef2 || item.gmpCode || item.cmaterialoidCode, //物料代码
                cmaterialoidName: item.cmaterialoidName, //物料名称
                materialspec: item.spec, //规格
                cproductoridName: item.cproductoridName, //生产厂家
                vbatchcode: item.vbatchcode, //批号
                nnum: item.nnum, //实收主数量
                vvendbatchcode: item.vvendbatchcode, //厂家批号
                dvalidate: item.dvalidate, //有效期
                vbdef48: item.vbdef48, //复验期,
                cunitidName: item.cunitidName //单位
              }));
              break;
            case 'otherInWu': //其他入库物料入库单
              source = res.data.otherIn?.[0] || {};
              table = res.data.otherIn.map(item => ({
                cmaterialoidCode: item.vbcdef2 || item.gmpCode || item.cmaterialoidCode, //物料代码
                cmaterialoidName: item.cmaterialoidName, //物料名称
                materialspec: item.spec, //规格
                cproductoridName: item.cproductoridName, //生产厂家
                vbatchcode: item.vbatchcode, //批号
                nnum: item.nnum, //实收主数量
                vvendbatchcode: item.vvendbatchcode, //厂家批号
                dvalidate: item.dvalidate, //有效期
                vbdef48: item.vbdef48, //复验期,
                cunitidName: item.cunitidName //单位
              }));
              break;
            case 'otherOut': //产品交接单
              source = res.data.otherProductOut?.[0] || {};
              table = res.data.otherProductOut.map(item => ({
                cmaterialoidCode: item.vbcdef2 || item.gmpCode || item.cmaterialoidCode, //物料代码
                cmaterialoidName: item.cmaterialoidName, //物料名称
                materialspec: item.spec, //规格
                dproducedate: item.dproducedate, //生产日期
                vbatchcode: item.vbatchcode, //批号
                nshouldnum: item.nshouldnum, //应转主数量
                dvalidate: item.dvalidate, //有效期
                cunitidName: item.cunitidName //单位
              }));
              break;
            case 'otherOutMore': //多行otherOut
              source = res.data.otherOut?.[0] || {};
              table = res.data.otherOut.map(item => ({
                cmaterialoidCode: item.vbcdef2 || item.gmpCode || item.cmaterialoidCode, //物料代码
                cmaterialoidName: item.cmaterialoidName, //物料名称
                materialspec: item.spec, //规格
                vbatchcode: item.vbatchcode, //批号
                xunum: item.nnum, //需求量
                dvalidate: item.dvalidate, //有效期
                vbdef48: item.vbdef48, //复验期,
                cunitidName: item.cunitidName, //单位,
                def9: item.def9 //物料属性
              }));
              break;
            case 'otherIn':
              source = res.data.otherIn?.[0] || {};
              table = res.data.otherIn.map(item => ({
                cmaterialoidCode: item.vbcdef2 || item.gmpCode || item.cmaterialoidCode, //物料代码
                cmaterialoidName: item.cmaterialoidName, //物料名称
                materialspec: item.spec, //规格
                cproductoridName: item.cproductoridName, //生产厂家
                vbatchcode: item.vbatchcode, //批号
                nnum: item.nnum, //实收主数量
                vvendbatchcode: item.vvendbatchcode, //厂家批号
                dvalidate: item.dvalidate, //有效期
                vbdef48: item.vbdef48, //复验期,
                cunitidName: item.cunitidName //单位
              }));
              break;
            case 'otherOutPrtBill': //普锐特
              source = res.data.otherProductMaterialsOut?.[0] || {};
              table = res.data.otherProductMaterialsOut.map(item => ({
                cmaterialoidCode: item.vbcdef2 || item.gmpCode || item.cmaterialoidCode, //物料代码
                cmaterialoidName: item.cmaterialoidName, //物料名称
                materialspec: item.spec, //规格
                vbatchcode: item.vbatchcode, //批号
                xunum: item.nnum, //需求量
                dvalidate: item.dvalidate, //有效期
                vbdef48: item.vbdef48, //复验期,
                cunitidName: item.cunitidName, //单位,
                def9: item.def9, //物料属性
              }));
              break;
            default:
              source = {}; // 默认兜底
              break;
          }
          let printData = {
            ...source,
            table: table
          };
          this.$refs.buyIndexRef.print(printData, this.formDataBill.modelBill);
        })
      },
    }
  }
</script>

<style>
</style>
